// site/site/site.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import location from '../../utils/location.js';
import notice from '../../utils/notice.js';
import formatDate from '../../utils/format_date';

const app = getApp();
let pageIndex1 = 1;
let pageIndex2 = 1;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: 0,
        tabs: [{
            title: '预订'
        }, {
            title: '全部'
        }],
        idx: 0,
        latitude: '',
        longitude: '',
        markers: [],
        city: '',
        list1: [],
        list2: [],
        isRefresh: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.name = '';
        this.windowInfo = wx.getWindowInfo();
        this.setData({
            statusBarHeight: this.windowInfo.statusBarHeight
        })
        if (options.idx) {
            this.setData({
                idx: options.idx
            })
        }
        location.getLocationAddress().then(res => {
            this.latitude = res.latitude;
            this.longitude = res.longitude;
            this.setData({
                latitude: res.latitude,
                longitude: res.longitude,
                [`city.label`]: res.city,
                markers: [{
                    latitude: res.latitude,
                    longitude: res.longitude,
                    iconPath: '/assets/loction.png',
                    width: 120,
                    height: 120,
                    anchor: {
                        x: 0.5,
                        y: 0.5
                    }
                }],
                points: [{
                    latitude: res.latitude,
                    longitude: res.longitude
                }]
            });
            wx.startPullDownRefresh();
        })

        notice.addNotice("GetCity", this.getCity, this);
    },

    onUnload() {
        pageIndex1 = 1;
        pageIndex2 = 1;
        notice.removeNotice("GetCity", this);
    },

    onPullDownRefresh() {
        this.setData({
            isRefresh: true
        })
        if (this.data.idx == 0) {
            pageIndex1 = 1;
            this.request1();
        } else {
            pageIndex2 = 1;
            this.request2();
        }
    },
    onReachBottom() {
        if (this.hasData1) {
            this.request1();
        } else if (this.hasData2) {
            this.request2();
        }
    },

    request1: function () {
        let param = {
            pageNo: pageIndex1,
            pageSize: 20,
            status: 1,
            publisherId: app.globalData.userInfo.id
        }

        network.getJson(api.couponOrderPage, param).then(res => {
            this.setData({
                isRefresh: false
            })
            if (pageIndex1 == 1) {
                this.setData({
                    list1: []
                })
            }
            let list = res.data.list;
            for (let i = 0; i < list.length; i++) {
                if (list[i].siteDO) {
                    list[i].siteDO.images = list[i].siteDO.images.split(',');
                }
                list[i].selected = false;
            }

            this.setData({
                list1: this.data.list1.concat(list)
            })
            if (pageIndex1 == 1 && this.data.list1.length > 0) {
                let item = this.data.list1[0];
                this.setData({
                    [`markers[1]`]: {
                        latitude: item.siteDO.lat,
                        longitude: item.siteDO.lng,
                        iconPath: '/assets/loc.png',
                        width: 25,
                        height: 30,
                        anchor: {
                            x: 0.5,
                            y: 1
                        }
                    },
                    ['points[1]']: {
                        latitude: item.siteDO.lat,
                        longitude: item.siteDO.lng
                    }
                })
            }
            if (this.data.list1.length < res.data.total) {
                this.hasData1 = true;
                pageIndex1 += 1;
            } else {
                this.hasData1 = false;
            }
        })
    },

    request2: function () {
        let param = {
            pageNo: pageIndex2,
            pageSize: 20,
            name: this.name,
            cityName: this.data.city.label
        }
        if (this.data.latitude) {
            param.lat = this.data.latitude;
            param.lng = this.data.longitude;
        }

        network.getJson(api.sitePage, param).then(res => {
            this.setData({
                isRefresh: false
            })
            if (pageIndex2 == 1) {
                this.setData({
                    list2: []
                })
            }
            let list = res.data.list;
            for (let i = 0; i < list.length; i++) {
                list[i].images = list[i].images.split(',');
                list[i].selected = false;
            }
            this.setData({
                list2: this.data.list2.concat(list)
            })

            if (this.data.list2.length < res.data.total) {
                this.hasData2 = true;
                pageIndex2 += 1;
            } else {
                this.hasData2 = false;
            }
        })
    },

    tabsChange(e) {
        this.setData({
            idx: e.detail
        })
        if (this.data.idx == 1) {
            location.getLocationAddress().then(res => {
                this.setData({
                    [`city.label`]: res.city
                });
                wx.startPullDownRefresh();
            })
        }

    },

    backTap() {
        wx.navigateBack();
    },

    chooseCity() {
        wx.navigateTo({
            url: '/site/city/city'
        })
    },

    getCity(e) {
        this.setData({
            city: e.city,
            isRefresh: true
        })
        pageIndex2 = 1;
        this.request2();
    },

    bindinput(e) {
        this.name = e.detail.value;
    },

    rightTap() {
        wx.startPullDownRefresh();
    },

    selectSite(e) {
        let index = e.currentTarget.dataset.index;
        for (let i = 0; i < this.data.list1.length; i++) {
            this.data.list1[i].selected = false;
        }
        this.data.list1[index].selected = true;
        this.setData({
            list1: this.data.list1
        })

        let site = this.data.list1[index].siteDO;
        notice.postNotice("SelectCoupon", {
            siteId: site.id,
            name: site.name,
            accommodate: site.accommodate,
            couponType: this.data.list1[index].couponType
        });
        wx.navigateBack();
    },

    goSiteDetails(e) {
        wx.navigateTo({
            url: '/site/site-details/site-details?id=' + e.currentTarget.dataset.id
        })
    },
})